<template>
    <view class="page-main">
        <liu-chooseAddress ref="liuChooseAddress" @submit="submitAddress" @detele="deteleAddress">
        </liu-chooseAddress>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				id:0,
                // 'RBWBZ-CV5RW-VX7RW-36ZQL-YZVYF-DDBMM' 此处为作者为方便H5体验所留测试用key,可以用来体验，但请勿在项目内直接使用
            }
        },
		onLoad(ops) {
			if(ops.id){
				this.id = ops.id
			}
		},
		onShow() {
			if(this.id>0){
				this.getinfo();
			}
		},
        methods: {
			getinfo(){
			
				var _this = this;
				
				_this.$api.request('/api/user/addressget',{id:this.id}, uni.getStorageSync('token'), function(res) {
					uni.hideLoading();
					if (res.code == 1) {
						console.log(res,777)
						
						if(res.data.agreeState == 0){
							res.data.agreeState = false;
						}
						
						let obj = res.data
						_this.$refs.liuChooseAddress.setData(obj)
					}
					uni.hideLoading();
				});
			},
            //添加地址成功回调
            submitAddress(e) {
				uni.showLoading({ title: '正在加载', mask: true });
				var _this = this;
				e.id = this.id
				_this.$api.request('/api/user/addressadd', e, uni.getStorageSync('token'), function(res) {
					uni.hideLoading();
					if (res.code == 1) {
						 uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						setTimeout(function(){
							uni.navigateBack({
							      delta: 1
							    })
						},500)
						
						return
					}
				
				});
				
            },
            //删除地址回调
            deteleAddress(e) {
				if(this.id > 0){
					uni.showLoading({ title: '正在加载', mask: true });
					var _this = this;
				
					_this.$api.request('/api/user/addressdel', {id:this.id}, uni.getStorageSync('token'), function(res) {
						uni.hideLoading();
						if (res.code == 1) {
							 uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							setTimeout(function(){
								uni.navigateBack({
								      delta: 1
								    })
							},500)
							return
						}
					
					});
				}
            },
            // 地址回显(编辑)调用此方法
            editAddress() {
                let obj = {
                    id: '1',
                    detailAddress: '', //门牌号(详细地址)
                    userName: '', //联系人姓名
                    userPhone: '', //手机号
                    lableName: '', //标签
                    sexType: '', //性别
                    title: '', //地址概述
                    address: '', //详细地址
                    longitude: '', //经度
                    latitude: '', //纬度
                    agreeState: '', //是否是默认地址
                }
                this.$refs.liuChooseAddress.setData(obj)
            }
        }
    }
</script>

<style lang="scss" scoped>
    .page-main {
        width: 100%;
        height: 100vh;
    }
</style>